<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 菜单管理 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="el-icon-delete" class="handle-del mr10" @click="delAllSelection">批量删除</el-button>
                <el-input v-model="query.Name" placeholder="菜单名称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="getData()">搜索</el-button>
                <el-button type="primary" icon="el-icon-plus" @click="AddVisible = true">添加菜单</el-button>
            </div>
            <el-table ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55"> </el-table-column>
                <el-table-column prop="menuName" label="菜单名称"> </el-table-column>
                <el-table-column prop="icon" label="lcon名称"> </el-table-column>
                <el-table-column prop="index" label="URL路径"> </el-table-column>
                <el-table-column prop="sortNo" label="排序"> </el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button size="mini" @click="saveEdit(scope.$index, scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <!-- 添加弹出框 -->
        <el-dialog title="添加" :visible.sync="AddVisible" width="30%">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="菜单名称" prop="menuName">
                    <el-input v-model="ruleForm.menuName"></el-input>
                </el-form-item>
                <el-form-item label="Icon名称" prop="icon">
                    <el-input v-model="ruleForm.icon"></el-input>
                </el-form-item>
                <el-form-item label="URL路径" prop="index">
                    <el-input v-model="ruleForm.index"></el-input>
                </el-form-item>
                <el-form-item label="上级菜单" prop="pId">
                    <el-select v-model="ruleForm.pId" placeholder="请选择上级菜单" :style="{ width: '100%', height: '100%' }">
                        <el-option style="height: 50%" value="00000000-0000-0000-0000-000000000000" label="根节点"></el-option>
                        <el-option style="height: 50%" value="">
                            <el-tree :data="TreeShape" :props="defaultProps" show-checkbox @check-change="handleCheckChange"> </el-tree>
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="AddVisible = false">取 消</el-button>
                <el-button type="primary" @click="Add()">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 编辑弹出框 -->
        <el-dialog title="添加" :visible.sync="UpdateVisible" width="30%">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="菜单名称" prop="menuName">
                    <el-input v-model="ruleForm.menuName"></el-input>
                </el-form-item>
                <el-form-item label="Icon名称" prop="icon">
                    <el-input v-model="ruleForm.icon"></el-input>
                </el-form-item>
                <el-form-item label="URL路径" prop="index">
                    <el-input v-model="ruleForm.index"></el-input>
                </el-form-item>
                <el-form-item label="上级菜单" prop="pId">
                    <el-select v-model="ruleForm.pId" placeholder="请选择上级菜单" :style="{ width: '100%', height: '100%' }">
                        <el-option style="height: 50%" value="00000000-0000-0000-0000-000000000000" label="根节点"></el-option>
                        <el-option style="height: 50%" value="">
                            <el-tree :data="TreeShape" :props="defaultProps" show-checkbox @check-change="handleCheckChange"> </el-tree>
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="UpdateVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleEdit()">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { getMenuTree, getMenuList, AddMenu, DeleteMenu, AllDeleteMenu, QueryIdMenu, UpdateMenu } from '../../api/menu.js';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                Name: ''
            },
            tableData: [],
            multipleSelection: [],
            ruleForm: {
                menuName: '',
                icon: '',
                index: '',
                deleteId: 0,
                sortNo: 0,
                pId: undefined,
                children: 'string',
            },
            TreeShape: [],
            defaultProps: {
                children: 'children',
                label: 'menuName',
                value: 'id'
            },
            delList: [],
            UpdateVisible: false,
            AddVisible: false,
            form: {},
        };
    },
    created() {
        this.getData();
        this.TreeData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            getMenuList(this.query).then((res) => {
                console.log(res);
                this.tableData = res.data;
            });
        },
        handleCheckChange(data, checked, indeterminate) {
            console.log(data, checked, indeterminate);
            this.ruleForm.pId = data.id;
        },
        TreeData() {
            getMenuTree().then((res) => {
                console.log(res);
                this.TreeShape = res.data;
            });
        },
        Add() {
            AddMenu(this.ruleForm).then((res) => {
                console.log(res);
                if (res.code == 200) {
                    this.$message.success('添加成功');
                    this.AddVisible = false;
                    this.getData();
                } else {
                    this.$message.success(res.message);
                }
            });
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    DeleteMenu(row.id).then((res) => {
                        console.log(res);
                        if (res.code == 200) {
                            this.$message.success('删除成功');
                            this.getData();
                        } else {
                            this.$message.success(res.Message);
                        }
                    });
                })
                .catch(() => {});
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        // 删除(批删)操作
        delAllSelection() {
            AllDeleteMenu(this.multipleSelection).then((res) => {
                        console.log(res);
                        if (res.code == 200) {
                            this.$message.success('删除成功');
                            this.getData();
                        } else {
                            this.$message.success(res.Message);
                        }
                    });
        },
        // 编辑操作
        handleEdit() {
            UpdateMenu(this.ruleForm).then((res) => {
                console.log(res);
                if (res.code == 200) {
                    this.$message.success('修改成功');
                    this.UpdateVisible = false;
                    this.getData();
                } else {
                    this.$message.success(res.message);
                }
            });
        },
        // 保存编辑
        saveEdit(index, row) {
            this.UpdateVisible = true;
            QueryIdMenu(row.id).then((res) => {
                console.log(res);
                this.ruleForm = res.data;
            });
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
